<template>
    <div style="min-width: 1500px;">
        <el-input class="search" v-model="queryCriteria.content" placeholder="" style="width: 900px; margin-right: 25px;"
            clearable />
        <el-button type="danger" @click="queryByPage">查询</el-button>
    </div>
    <!-- 条件导航 -->
    <div class="comment-menu">
        <div class="first-menu">
            <div class="menu-left">
                <span>分类</span>
            </div>
            <template v-for="(data, i) in classification">
                <div class="menu-right">
                    <div :class="ClassificationSubscript == i ? 'change-bg' : ''" @click="changeClassification(i)">
                        <span>{{ data }}</span>
                    </div>
                </div>
            </template>

        </div>
        <div class="division"></div>
        <div class="second-menu">
            <div class="menu-left">
                <span>排序</span>
            </div>
            <template v-for="(data, index) in collation">
                <div class="menu-right">
                    <div :class="CollationSubscript == index ? 'change-bg' : ''" @click="changeCollation(index)">
                        <span>{{ data }}</span>
                    </div>
                </div>
            </template>

        </div>
    </div>
    <div class="operation">
        <div style="font-weight: bolder;">
            评论数量：
            <span>{{commentCount}}</span>
            条
        </div>
        <el-button type="danger" @click="deleteComment(-1)">删除选中</el-button>
    </div>

    <!-- 表格 -->
    <div style="max-width: 1600px;min-width: 1000px;margin: auto;">
        <el-row style="height: 550px;padding-top: 20px;">
            <el-table :data="pageList.recodes" border style="width: 100%;" @selection-change="handleSelectionChange"
                @select='onTableSelect' :row-key="getRowKeys" :header-cell-style="{ 'text-align': 'center' }" 
                :cell-style="{ 'text-align': 'center' }" :row-class-name="rowStyle" >
                <el-table-column fixed="left" type="selection" width="55" :reserve-selection="true" />
                <el-table-column type="index" label="编号" width="60" />
                <el-table-column prop="nickName" label="用户名" width="150" />
                <el-table-column prop="userName" label="手机号" width="120" />
                <el-table-column show-overflow-tooltip="true" prop="title" label="所属文章" width="250" />
                <el-table-column show-overflow-tooltip="true" prop="content" label="评论内容" width="350" />
                <el-table-column prop="createTime" label="发布时间" width="180" />
                <el-table-column prop="likes" label="点赞数" width="70" />
                <el-table-column prop="state" label="是否已删除" width="100" />
                <el-table-column fixed="right" label="操作">
                    <template #default="scope">

                        <template v-if="scope.row.state == 0">
                            <el-button type="danger" size="small" @click="deleteTip(scope.row.id)">删除</el-button>
                        </template>
                        <template v-if="scope.row.state == 1">
                            <el-button type="success" size="small" @click="recoverComment(scope.row.id)">恢复</el-button>
                        </template>

                    </template>
                </el-table-column>
            </el-table>
            <div class="page">
                <el-pagination background layout="prev, pager, next" :total="pageList.count"
                    :page-size="pageList.pageSize" :current-page="pageList.currentPage" @current-change="changepage">
                </el-pagination>
            </div>
        </el-row>

    </div>

    <!-- 是否删除对话框 -->
    <el-dialog v-model="dialogDeleteVisible" title="是否删除该评论" width="30%">
        <el-button @click="dialogDeleteVisible = false">否</el-button>
        <el-button type="primary" @click="sureDelete">是</el-button>
    </el-dialog>
</template>

<script>
import comment from '@/api/comment/comment'
import { ElMessage } from 'element-plus';
export default {
    name: 'DiscussManage',

    data() {
        return {
            ids: [], //选中删除所需的id
            dialogDeleteVisible: false,
            ClassificationSubscript: 0,
            CollationSubscript: 0,
            collation: ["最新发布", "最多点赞"],
            classification: ["全部", "未删除", "已删除"],
            queryCriteria: {
                content: '',
                classificationType: -1,
                collationType: 0,
                current: 1,
                sort: false
            },
            pageList: '',
            comment: '',
            commentCount:'',
        };
    },

    created(){
        this.queryByPage()
    },
    methods: {
        // 分类的切换
        changeCollation(index) {
            this.queryCriteria.collationType = index
            if (this.CollationSubscript == index) {
                this.queryCriteria.sort = !this.queryCriteria.sort
            } else {
                this.CollationSubscript = index
            }
            this.queryByPage();
        },
        // 排序的切换
        changeClassification(i) {
            this.queryCriteria.classificationType = i - 1
            if (this.ClassificationSubscript == i) {
                this.queryCriteria.sort = !this.queryCriteria.sort
            } else {
                this.ClassificationSubscript = i
            }
            this.queryByPage();
        },
        //分页查询
        queryByPage() {
            comment.queryByPage(this.queryCriteria).then(re => {
                //如果查询当前页无内容，再次发送请求获取上一页的内容
                if (re.data.data.pages.recodes.length == 0 && this.queryCriteria.current > 1) {
                    this.queryCriteria.current--;
                    this.queryByPage()
                }
                this.pageList = re.data.data.pages
                this.commentCount = re.data.data.pages.count
            })
        },
        changepage(newPage) {
            this.queryCriteria.current = newPage
            this.queryByPage();
        },
        //是否删除
        deleteTip(id){
            if (id >= 0) {
                this.ids.push(id)
            }
            this.dialogDeleteVisible = true;
        },
        //确认删除
        sureDelete(){
            comment.deleteMultipleComment(this.ids).then(re => {
                if (re.data.code == 20000) {
                    ElMessage.success("删除成功！")
                    this.queryByPage()
                } else {
                    ElMessage.error("删除失败！")
                }
            })
            this.dialogDeleteVisible = false;
        },
        //恢复评论
        recoverComment(id) {
            comment.recoverComment(id)
                .then(re => {
                    if (re.data.code == 20000) {
                        ElMessage.success("操作成功！")
                        this.queryByPage()
                    } else {
                        ElMessage.error("操作失败！")
                    }
                })
        },
        getRowKeys(row) {
            return row.id
        },
        handleSelectionChange(val) {
            this.ids = []
            for (let index = 0; index < val.length; index++) {
                this.ids.push(val[index].id);
            }
        },
        rowStyle({row,rowIndex}){
            if(row.state == 1){
                return 'warning-row'
            }            
        }
    },
};
</script>

<style scoped>
.comment-menu {
    margin: auto;
    margin-top: 25px;
    width: 90%;
    max-width: 1400px;
    min-width: 1300px;
    height: 20%;
    background-color: #f56c6c;
}

.comment-menu span {
    color: #eee;
}

.comment-menu .menu-right {
    cursor: pointer;
}

.first-menu {
    height: 50%;
    display: flex;
    justify-content: flex-start;
    align-items: center;
}

.menu-left {
    margin: 0 50px;
}

.menu-right {
    margin-right: 25px;
}

.menu-right {
    text-align: center;
}

.menu-right span {
    margin: 0 15px;
}

.change-bg {
    text-align: center;
}

.change-bg {
    background-color: #fff;
    border-radius: 4px;
}

.change-bg span {
    color: #f56c6c;
}

.division {
    width: 95%;
    margin: auto;
    border-bottom: 1px dashed #fff;
}

.second-menu {
    height: 50%;
    display: flex;
    justify-content: flex-start;
    align-items: center;
}

.search :deep(.is-focus) {
    box-shadow: 0 0 0 1px #f56c6c;
}

.operation {
    margin: auto;
    margin-top: 25px;
    max-width: 1400px;
    min-width: 1300px;
    display: flex;
    justify-content: space-between
}
.page{
    margin: 0 auto;
}
.el-row{
    justify-content: flex-start !important;
}

</style>